package controllers;

import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import services.auto.AutoService;

import beans.AutoVO;

@Controller
public class AltaAutoController {

	@Autowired
	private AutoService autoService;
	
	@RequestMapping(value="public/altaAuto", method = RequestMethod.GET)
	public ModelAndView alta(@ModelAttribute AutoVO autoNuevo){
		ModelAndView mv = new ModelAndView();
		System.out.println(autoNuevo.getModelo());
		System.out.println(autoNuevo.getPatente());
		System.out.println(autoNuevo.getColor());
		System.out.println(autoNuevo.getMarca());
		System.out.println(autoNuevo.getDetalle());
		
		//Sin IOC
		//AutoServiceImpl autoService = new AutoServiceImpl();
		try {
			autoService.insertAuto(autoNuevo);
			mv.setViewName("altaOK");
		} catch (SQLException e) {
			mv.addObject("errorParam", "Error al insertar auto nuevo");
			mv.setViewName("error");
			e.printStackTrace();
		}
		return mv; 
	}

	public void setAutoService(AutoService autoService) {
		this.autoService = autoService;
	}
}
